<?php
/**
 * @package		Joomla.Site
 * @subpackage	com_content
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

// Component Helper
jimport('joomla.application.component.helper');
/**
 * Contact Component Category Tree
 *
 * @static
 * @package		Joomla.Site
 * @subpackage	com_contact
 * @since 1.6
 */
class shoppingHelper
{
	public function getProducts()
	{
		$url = "http://50.28.60.118:8983/solr/browse?&wt=xml&q=";
		$xmlstring =  shoppingHelper::post_curl($url);
		jimport('joomla.utilities.simplexml');
		$xml = new JSimpleXML();
		$xml->loadString($xmlstring,'products');
		
		$docs =  $xml->document->_children[1]->_children;
		$products = array();
		foreach($docs as $doc)
		{
			$attrs = $doc->_children;
			$child = array();
			foreach($attrs as $attr)
			{
				
				if($attr->_attributes["name"]=="image")
				{
					$imgs = array();
					foreach($attr->_children as $ims)
					{
						$imgs[] = $ims->_data;
					}
					$child[$attr->_attributes["name"]] =  $imgs;
				}
				else
					$child[$attr->_attributes["name"]] =  $attr->_data;
				
			}
			$products[$child["id"]] = $child;
		}
		//unset($_SESSION['shop_products']);
		if(!$_SESSION['shop_products'])
			$_SESSION['shop_products'] = $products;
		
	}
	
	
	private function post_curl($url) 
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
		$result = curl_exec($ch);
		curl_close ($ch);
		return $result;

	}

	private function get_curl($url) 
	{
	    $ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_GET, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
		$result = curl_exec($ch);
		curl_close ($ch);
	    return $result;
	}
	
	function formatTel($tel)
	{
		$str =  str_replace("Mobile: ","",$tel);
		if(strpos($str,"Telephone"))
			$str =  str_replace("Telephone: ",", ",$str);
		
		return $str;
	}
	
}
